package com.yckj.activechicken.business.purchase.controller;

import com.yckj.activechicken.business.purchase.model.PurApplyBill;
import com.yckj.activechicken.business.purchase.model.vo.PurApplyBillVo;
import com.yckj.activechicken.business.purchase.model.vo.PurApplyTaskVo;
import com.yckj.activechicken.business.purchase.service.IPurApplyService;
import com.yckj.activechicken.business.system.model.SysUser;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import com.yckj.activechicken.framework.security.model.LoginUser;
import com.yckj.activechicken.framework.web.controller.BaseController;
import com.yckj.activechicken.framework.web.domain.AjaxResult;
import com.yckj.activechicken.framework.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.purchase.controller
 * </p>
 * <p/>
 * <p>
 * Description: 描述【采购申请管理控制器】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/12 10:15
 */
@RestController
@RequestMapping("/purchase/apply")
public class PurApplyController extends BaseController{

    @Autowired
    private IPurApplyService purApplyService;

    /**
     * 获取当前登录者的用户信息
     * @return
     */
    @GetMapping("/queryUserInfo")
    public AjaxResult queryUserInfo(){
        Map<String,Object> outMap = new HashMap<>(5);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();

        outMap.put("applyUser",sysUser.getUserName());
        outMap.put("applyUserName",sysUser.getLoginName());
        outMap.put("deptName",sysUser.getDept().getDeptName());
        outMap.put("deptId",sysUser.getDept().getDeptId());

        outMap.put("posts",sysUser.getPosts());
        outMap.put("roles",sysUser.getRoles());
        return AjaxResult.success(outMap);
    }


    /**
     * 保存采购申请单
     * @param purApplyBillVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:addApplyBill')")
    @PostMapping("/addApplyBill")
    public AjaxResult addApplyBill(@RequestBody PurApplyBillVo purApplyBillVo){
        String purApplyId = purApplyService.addApplyBill(purApplyBillVo);
        Map<String,String> outMap = new HashMap<>(1);
        outMap.put("purApplyId",purApplyId);
        return AjaxResult.success(outMap);
    }

    /**
     * 分页查询采购申请单
     * @param purApplyBill
     * @return
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:listApplyBill')")
    @GetMapping("/listApplyBill")
    public TableDataInfo listApplyBill(PurApplyBill purApplyBill) {
        startPage();
        List<PurApplyBill> list = purApplyService.listApplyBill(purApplyBill);
        return getDataTable(list);
    }

    /**
     * 通过申请单号删除申请单
     * @param purApplyBill
     * @return
     */
    @PostMapping("/delApplyBill")
    public AjaxResult delApplyBill(@RequestBody PurApplyBill purApplyBill){
        return toAjax(purApplyService.delApplyBill(purApplyBill));
    }

    /**
     * 查询该申请单的用户信息以及申请单货品清单
     * @param purApplyBill
     * @return
     */
    @PostMapping("/queryGoodsBillAndUserInfo")
    public AjaxResult queryGoodsBillAndUserInfo(@RequestBody PurApplyBill purApplyBill){
        Map<String,Object> outMap = purApplyService.queryGoodsBillAndUserInfo(purApplyBill);
        return AjaxResult.success(outMap);
    }

    /**
     * 修改采购申请单
     * @param purApplyBillVo
     * @return
     */
    @PostMapping("/modifyApplyBill")
    public AjaxResult modifyApplyBill(@RequestBody PurApplyBillVo purApplyBillVo){
        String username = SecurityUtils.getUsername();

        if(!username.equals(purApplyBillVo.getApplyUser())){
            return AjaxResult.error("只有采购申请者能够修改申请单，请确认身份！");
        }

        purApplyService.modifyApplyBill(purApplyBillVo);
        return AjaxResult.success();

    }

    /**
     * 采购申请单发起审批
     * @param purApplyBill
     * @return
     */
    @PostMapping("/submitApproval")
    public AjaxResult submitApproval(@RequestBody PurApplyBill purApplyBill){
        purApplyService.submitApproval(purApplyBill);
        return AjaxResult.success();
    }

    /**
     * 查询采购申请单审批的待办列表
     * @param purApplyBill
     * @return
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:listTodoPurApply')")
    @PostMapping("/listTodoPurApply")
    public AjaxResult listTodoPurApply(@RequestBody PurApplyBill purApplyBill){
        List<PurApplyTaskVo> purApplyTaskVoList = purApplyService.queryTodoPurApplyList(purApplyBill);
        return AjaxResult.success(purApplyTaskVoList);
    }

    /**
     * 采购申请单审批
     * @param purApplyTaskVo
     * @return
     */
    @PostMapping("/approvalApply")
    public AjaxResult approvalApply(@RequestBody PurApplyTaskVo purApplyTaskVo){
        purApplyService.approvalApply(purApplyTaskVo);
        return AjaxResult.success();
    }

    /**
     * 分页查询历史已办任务
     * @param purApplyId
     * @param applyTitle
     * @param deptName
     * @return
     */
    @PreAuthorize("@ss.hasPermi('purchase:apply:queryApplyApprovalHisList')")
    @GetMapping("/queryApplyApprovalHisList")
    public TableDataInfo queryApplyApprovalHisList(String purApplyId,String applyTitle,String deptName) {
        startPage();
        Map<String,String> queryMap = new HashMap<>(4);
        queryMap.put("purApplyId",purApplyId);
        queryMap.put("applyTitle",applyTitle);
        queryMap.put("deptName",deptName);
        String username = SecurityUtils.getUsername();
        queryMap.put("username",username);
        List<PurApplyTaskVo> list = purApplyService.queryApplyApprovalHisList(queryMap);
        return getDataTable(list);
    }

}
